package com.angis.fx.activity.service;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import com.angis.fx.data.DraftInfo;
import com.angis.fx.db.CheckDraftDBHelper;
import com.angis.fx.handler.login.UpdateHandler;
import com.angis.fx.util.ContentValuesUtil;

public class CheckDutyService extends Service {

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		try {
			DraftInfo lDraftInfo = (DraftInfo) intent.getExtras().getSerializable("CHECKDUTY");
			lDraftInfo.setDatastatus("1");
			int flag = UpdateHandler.checkdutyUpdate(lDraftInfo);
			if (flag == -1) {
				Toast.makeText(this, "服务器连接错误!", Toast.LENGTH_SHORT).show();
			} else if (flag == 0) {
				Toast.makeText(this, "上传错误!", Toast.LENGTH_SHORT).show();
			} else if (flag == 1) {
				CheckDraftDBHelper lDBHelper = new CheckDraftDBHelper(CheckDutyService.this);
				ContentValues lValues = ContentValuesUtil.convertCheckDraft(lDraftInfo);
				if( intent.getExtras().getBoolean("isSave"))
				{
					//lDraftInfo.setDatastatus("1");
					lDBHelper.updateDraft(lDraftInfo);
				}else{
					lDraftInfo.setDatastatus("1");
					lDBHelper.insert(lValues);
				}
				lDBHelper.closeDB();
				Toast.makeText(this, "上传完成!", Toast.LENGTH_SHORT).show();
				
				Intent resultIntent = new Intent("upLoadResult");
				resultIntent.putExtra("isCompleteUpload", true);
				this.sendBroadcast(resultIntent);
			}
		} catch (Exception e) {
			Intent resultIntent = new Intent("upLoadResult");
			resultIntent.putExtra("isCompleteUpload", false);
			this.sendBroadcast(resultIntent);
		}finally {this.stopSelf();}
//		lIntent.putExtra("uploadresult", "1");
//		this.sendBroadcast(lIntent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

}
